
public class RandomArray {
private int[] array; // instance variable
public RandomArray(int size) {
array = new int[size];
for(int i=0; i < array.length; i++){
array[i]=(int)(Math.random() * 10);
}
}
public void printArray() {
for(int i=0; i < array.length; i++){
System.out.print(""+array[i]+" ");
}
}
public int calcSum() {
int sum=0;
for(int i=0; i < array.length; i++){
sum=sum+array[i];}
return sum;
}
public double calcMean() {
int sum=0;
for(int i=0; i < array.length; i++){
sum=sum+array[i];}
double mean= sum/array.length;
return mean;
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
System.exit(-1);
}
RandomArray test = new RandomArray(Integer.parseInt(args[0]));
test.printArray();
System.out.println("Sum: " + test.calcSum());
System.out.println("Mean: " + test.calcMean());
}
}
